// pages/consult/consult.js
import { BASE_URL } from '../../http/config';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    teacherAll: [],
    pageNo: 1,
    pageTotal: 1, // 总页数
    isLoading: false, // 防止重复请求
    // img:BASE_URL,
    img:'',
        address:null,
        expertList:[],
        consultGrade:null,
        mannerid:null
  },
  getTeacherList() {
    if (this.data.isLoading) return;

    this.setData({
      isLoading: true
    });
    const _this = this;
    // console.log(this.data.address);
    wx.request({
      url: `${BASE_URL}/consult/wx?pageNum=`+this.data.pageNo,
      method:'POST',
      data:{
        address:this.data.address,
        expertList:this.data.expertList,
        consultGrade:this.data.consultGrade,
        mannerid:this.data.mannerid
      },
      header: {
        'content-type': 'application/json' // 显式设置请求头
      },
      success(res) {
        console.log(res);
        _this.setData({
          teacherAll: _this.data.pageNo === 1 ? res.data.data.list : [..._this.data.teacherAll, ...res.data.data.list],
          pageTotal: res.data.data.pageTotal || 1,
          isLoading: false
        });
        if (_this.data.pageNo === 1) {
          wx.stopPullDownRefresh();
          wx.hideNavigationBarLoading();
        }
      },
      fail() {
        _this.setData({
          isLoading: false
        });
        wx.stopPullDownRefresh();
        wx.hideNavigationBarLoading();
      }
    })
  },
  handleImageClick: function(event) {
    // 获取传入的id
    const id = event.currentTarget.dataset.id;
    console.log('点击的图片ID:', id);
    wx.navigateTo({
      url: '/consult/pages/consultDetail/consultDetail?id=' + id
    })
  },
  onReachBottom() {
    if (this.data.pageNo >= this.data.pageTotal) {
      wx.showToast({
        title: '没有更多数据!',
        icon: 'none'
      })
      return;
    }
    console.log('加载下一页数据')
    this.setData({
      pageNo: this.data.pageNo + 1
    })
    this.getTeacherList()
  },
  onPullDownRefresh() {
    console.log('上拉刷新');
    wx.showNavigationBarLoading(); // 显示顶部加载动画
    this.setData({
      pageNo: 1
    });
    this.setData({
      address:null,
      expertList:[],
      consultGrade:null,
      mannerid:null
    })
    this.getTeacherList()
  },
  toFind(){
    wx.navigateTo({
      url: '/consult/pages/find/find',
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if(options.address!=null){
      console.log("options",options);
      const expertListStr = options.expertList
      const expertList = expertListStr.split(",").map(item => {
        const num = parseInt(item, 10);
        return isNaN(num) ? null : num; // 或者根据需要处理
      });
      this.setData({
        address:options.address,
        expertList:expertList,
        consultGrade:options.consultGrade,
        mannerid:options.mannerid
      })
      // console.log(this.data.address);
    }


    this.getTeacherList()
    

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },



  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})